package im.vinci.server.songbook.service;

import im.vinci.server.common.exceptions.VinciException;
import im.vinci.server.common.exceptions.error.ErrorCode;
import im.vinci.server.songbook.domain.PlayList;
import im.vinci.server.songbook.domain.wrapper.GetHeadPhoneSongbookResponse;
import im.vinci.server.songbook.persistence.SongbookMapper;
import im.vinci.server.utils.BizTemplate;
import im.vinci.server.utils.JsonUtils;
import im.vinci.server.utils.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by mingjie on 16/12/5.
 */
@Service
public class SongbookService {

    @Value("${headphone.songbook.version}")
    private int serverVersion;

    @Autowired
    private SongbookMapper songbookMapper;

    public GetHeadPhoneSongbookResponse getHeadPhoneSongbook(int version) {
        return new BizTemplate<GetHeadPhoneSongbookResponse>("SongbookService.getHeadPhoneSongbook") {
            @Override
            protected void checkParams() throws VinciException {
                if (version < 0) {
                    throw new VinciException(ErrorCode.ARGUMENT_ERROR, "version不能小于0", "version不能小于0");
                }
            }

            @Override
            protected GetHeadPhoneSongbookResponse process() throws Exception {
                GetHeadPhoneSongbookResponse response = new GetHeadPhoneSongbookResponse();
                response.setVersion(serverVersion);

                if (version != serverVersion) {
                    List<PlayList> data = songbookMapper.getSongbookData();
                    response.setData(data);
                }
                return response;
            }
        }.execute();
    }
}
